package pers.cz.testNg;

import com.jayway.jsonpath.JsonPath;
import org.testng.Assert;
import org.testng.Reporter;

/**
 * 自定义检查点
 * @program: PostGril_1.0
 * @description: MyAssert
 * @author: Cheng Zhi
 * @create: 2021-11-05 17:21
 **/
public class MyAssert extends Assert {

    public static void check(Boolean isPass, Exception e) {
        if (isPass) {

        } else {
            throw new AssertionError(e.getMessage());
        }
    }

    /**
     * 异常检查点
     * @param actual 实际的
     * @param expected 预期的
     */
    public static void exceptionCheck(Object actual, Object expected) {


    }

    /**
     * 不为空检查点
     * @param actual 实际的
     * @param expected 预期的
     */
    public static void notNullCheck(Object actual, Object expected) {

        if (actual.toString().contains("") || actual.toString().contains("null") || actual.toString().contains("NULL")) {
            throw new AssertionError("实际结果存在空值");
        }
        Reporter.log("不为空点检查通过");
    }

    /**
     * 包含检查点
     * @param actual 实际的
     * @param expected 预期的
     */
    public static void contentCheck(Object actual, Object expected) {

        if (actual.toString().contains(expected.toString())) {

            Reporter.log("包含点检查通过");
        } else {
            throw new AssertionError("实际结果中不包含预期结果");
        }
    }

    /**
     * jsonPath检查点
     * @param actual 实际的
     * @param expected 检查表达式
     */
    public static void jsonPathCheck(String actual, String expected) {


        //Boolean o = postGirlParser(actual, expected);
        Boolean o = PostGirlParser.blockLexer(actual, expected);

        if (o) {

        } else {
            throw new AssertionError("不通过");
        }


    }

    /**
     * 自定义解析器，返回jsonpath表达式和操作内容
     * @param expression
     * @return
     */
    private static Boolean postGirlParser(String jsonStr, String expression) {

        String include = "⊇";
        if (!expression.contains(include)) {
            throw new RuntimeException("表达式解析失败");
        }

        String[] split = expression.split(include); // 返回了表达式
        Object read = JsonPath.read(jsonStr, split[0].trim());
        if (read.toString().contains(split[1].trim())) {
            return true;
        }

        return false;
    }
}
